package cn.edu.njust.alg.leetcode;

import cn.edu.njust.alg.ArrayUtils;

public class Leetcode_66_plus_one {

	/*
	 * 
	 * Given a non-negative number represented as an array of digits, plus one
	 * to the number.
	 * 
	 * The digits are stored such that the most significant digit is at the head
	 * of the list.
	 */

	public static int[] plusOne(int[] digits) {
		int[] res = digits.clone();
		int c = 1;
		for (int i = digits.length - 1; i >= 0; i--) {
			res[i] = (digits[i] + c) % 10;
			c = (digits[i] + c) / 10;
			if (c == 0) {
				break;
			}
		}
		if (c != 0) {
			int[] r = new int[res.length + 1];
			System.arraycopy(res, 0, r, 1, res.length);
			r[0] = c;
			res = r;
		}

		return res;
	}

	public static void main(String[] args) {
		int[] digits = { 1, 0 };
		ArrayUtils.printArray(plusOne(digits));
	}

}
